<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/8/13
 * Time: 14:49
 */

namespace app\admin\controller\staff;

use app\admin\model\StaffCard;
use app\admin\model\StaffSkill;
use app\admin\model\StaffSkillValue;
use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;
use think\Lang;

class Staffmange extends Backend
{
	
	protected $model = null;
	
	public function _initialize()
	{
		parent::_initialize();
		$this->model = model('Staff');
		$serverdayslist = db('serverdays')->select();
		$gradelist = db('grade')->select();
		$provincelist = db('province')->select();
		$animalslist = db('animalsyear')->select();
		$this->view->assign("serverdaysList", $serverdayslist);
		$this->view->assign("animalsList", $animalslist);
		$this->view->assign("gradeList", $gradelist);
		$this->view->assign("provincelist", $provincelist);
		if(isset($_GET['typeid']))$this->view->assign('typeid',$_GET['typeid']);
	}
	
	/**
	 * 查看员工列表
	 */
	public function index($id=null)
	{
		/**
		 * 此页面为员工管理页面的通用页面，以后可以根据用户的相应操作来重新初始化页面并修改相应的信息。
		 * 加载页面需要的数据资源，并加载到页面中
		 */
		if ($this->request->isAjax()) {
			list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
			$total = $this->model->with('grade')
				->where($where)->where("type",$id)
				->order($sort, $order)
				->count();
			$list = $this->model->with('grade')
				->where($where)->where("type",$id)
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
			$result = array("total" => $total, "rows" => $list);
			
			return json($result);
		}
		$this->view->assign('typeid',$id);
		return $this->view->fetch();
	}
	
	public function add()
	{
		if($this->request->isPost())
		$_POST['row']['age']=birthday($_POST['row']['birthday']);
		if ($this->request->isPost())
		{
			$this->code = -1;
			$params = $_POST['row'];
			if ($params)
			{
				foreach ($params as $k => &$v)
				{
					$v = is_array($v) ? implode(',', $v) : $v;
				}
				try
				{
					//是否采用模型验证
					if ($this->modelValidate)
					{
						$name = basename(str_replace('\\', '/', get_class($this->model)));
						$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
						$this->model->validate($validate);
					}
					$result = $this->model->save($params);
					if ($result !== false)
					{
						$this->code = 1;
					}
					else
					{
						$this->msg = $this->model->getError();
					}
				}
				catch (\think\exception\PDOException $e)
				{
					$this->msg = $e->getMessage();
				}
			}
			else
			{
				$this->msg = __('Parameter %s can not be empty', '');
			}
            $this->view->assign('typeid',$_GET['typeid']);
			return;
		}
		return $this->view->fetch();
	}
	public function edit($ids = NULL)
	{
		if($this->request->isPost())
		$_POST['row']['age']=birthday($_POST['row']['birthday']);
		$row = $this->model->get($ids);
		if (!$row)
			$this->error(__('No Results were found'));
		if ($this->request->isPost())
		{
			$this->code = -1;
			$params = $_POST['row'];
			if ($params)
			{
				foreach ($params as $k => &$v)
				{
					$v = is_array($v) ? implode(',', $v) : $v;
				}
				try
				{
					//是否采用模型验证
					if ($this->modelValidate)
					{
						$name = basename(str_replace('\\', '/', get_class($this->model)));
						$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
						$row->validate($validate);
					}
					$result = $row->save($params);
					if ($result !== false)
					{
						$this->code = 1;
					}
					else
					{
						$this->msg = $row->getError();
					}
				}
				catch (think\exception\PDOException $e)
				{
					$this->msg = $e->getMessage();
				}
			}
			else
			{
				$this->msg = __('Parameter %s can not be empty', '');
			}
			
			return;
		}
		$this->view->assign("row", $row);
		return $this->view->fetch();
	}
	
	/**
	 * 管理员工证件
	 */
	public function card($ids = null)
	{
		//此函数查看员工的个人证件，并显示出来通过此函数可以快速处理个人证件信息，包括展示和修改
		
		/**
		 * 点击查看个人证件时，
		 * step1:读取证件模型，首先读取证件所有信息，并加载到html页面中
		 * step2:读取id为x的员工模型，并把员工存在的现有的数据放入到相应的页面控件中
		 * 修改个人证件时，
		 * step1：点击上传修改相应的图片
		 * step2：点击确定，数据库更新员工修改的页面并重新加载页面
		 */
		if($this->request->isPost()){//是否为表单提交,如果是就修改数据库
			$ids=input('post.userid');
			$carddata=$_POST['card_id'];
			$savedata=[];
			$staffcardmodel=new StaffCard;
			$this->code = 1;
			$this->msg ="";
			foreach ($carddata as $key=>$value){//初始化更新数据
				$item=["staff_id"=>$ids,"card_id"=>$key,"pic_src"=>$value];
				array_push($savedata,$item);
			}
			if($staffcardmodel->where('staff_id',$ids)->find()!=null){//判断是否存在数据如果存在则保存，不存在则新增
				foreach ($carddata as $key=>$value){
					$item=["pic_src"=>$value];
					$result=$staffcardmodel->save($item,["staff_id"=>$ids,"card_id"=>$key]);
					if ($result === false)
					{
						$this->msg = $this->model->getError();
					}
				}
			}
			else{
				$result=$staffcardmodel->saveAll($savedata);
				if ($result === false)
				{
					$this->msg = $this->model->getError();
				}
			}
		
			return;
		}
		$cardlist = db('Card')->select();//加载证件类型列表
		$staffcard = db('StaffCard')->where('staff_id', $ids)->select();//加载id员工的证件信息
		foreach ($cardlist as $value){
			$data['cardid' . $value['id']]['id']=$value['id'];
			$data['cardid' . $value['id']]['znname']=$value['zn_name'];
			$data['cardid' . $value['id']]['pic_src']='';
		}
		foreach ($staffcard as $value) {
			$data['cardid' . $value['card_id']]['pic_src'] = $value['pic_src'];
		}
		$this->view->assign("userid", $ids);
		$this->view->assign("data", $data);
		return $this->view->fetch();
	}
	/**
	 * 设置员工相应技能等级，前台相应展示雷达图
	 */
	public function skillgrade($id=null){
		//此函数的主要功能为显示并修改技能数值
		/**
		 * 点击查看个人等级点
		 * step1：读取等级模型，首先读取等级的所有信息，并加载到html页面中
		 * step2：读取id为x的员工模型，并把员工存在现有数据放入相应的页面控件中，如果存在初始化为0
		 * 修改个人等级点
		 * step1：修改相应的数值
		 * step2：点击确定，数据库更新
		 */
		if($this->request->isPost()){//是否为表单提交,如果是就修改数据库
			$id=input('post.userid');
			$skilldata=$_POST['skill_id'];
			$savedata=[];
			$staffskillmodel=new StaffSkillValue();
			$this->code = 1;
			$this->msg ="";
			foreach ($skilldata as $key=>$value){//初始化更新数据
				$item=["staff_id"=>$id,"skill_id"=>$key,"value"=>$value];
				array_push($savedata,$item);
			}
			if($staffskillmodel->where('staff_id',$id)->find()!=null){//判断是否存在数据如果存在则保存，不存在则新增
				foreach ($skilldata as $key=>$value){
					$item=["value"=>$value];
					$result=$staffskillmodel->save($item,["staff_id"=>$id,"skill_id"=>$key]);
					if ($result === false)
					{
						$this->msg = $this->model->getError();
					}
				}
			}
			else{
				$result=$staffskillmodel->saveAll($savedata);
				if ($result === false)
				{
					$this->msg = $this->model->getError();
				}
			}
			
			return;
		}
		$skilllist = db('StaffSkill')->select();//加载证件类型列表
		$staffskill = db('StaffSkillValue')->where('staff_id', $id)->select();//加载id员工的证件信息
		foreach ($skilllist as $value){
			$data['skill_id' . $value['id']]['id']=$value['id'];
			$data['skill_id' . $value['id']]['znname']=$value['zn_name'];
			$data['skill_id' . $value['id']]['value']=0;
		}
		foreach ($staffskill as $value) {
			$data['skill_id' . $value['skill_id']]['value'] = $value['value'];
		}
		$this->view->assign("userid", $id);
		$this->view->assign("data", $data);
		return $this->view->fetch();
	}
	
}